package com.xu.ai.model.openai.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 聊天客户端配置
 *
 * @author xuguan
 * @since 2025/9/15
 */
@Configuration
public class ModelConfig {
    private static final String DEFAULT_SYSTEM = """
            你是基于openai大模型开发的智能ai系统.
            你是xu-ai智能聊天机器人.
            你的职责是回答和解决用户的问题, 你需要保持耐心专业, 给出正确的回答.
            """;

    /**
     * 会话记忆
     */
    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder().build();
    }

    /**
     * 向量存储
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }

    /**
     * 聊天客户端
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder chatClientBuilder) {
        // 工具
        // final DateTimeTool dateTimeTool = new DateTimeTool();

        return chatClientBuilder
                // 默认系统文本
                .defaultSystem(DEFAULT_SYSTEM)
                // 添加日志
                .defaultAdvisors(new SimpleLoggerAdvisor())
                // 聊天记忆
                // .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                // RAG增强检索
                // .defaultAdvisors(QuestionAnswerAdvisor.builder(vectorStore).build())
                // 工具
                // .defaultTools(dateTimeTool)
                .build();
    }
}
